package cn.iocoder.yudao.module.resource.controller.admin.tagclassify;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.resource.controller.admin.tagclassify.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.tagclassify.TagClassifyDO;
import cn.iocoder.yudao.module.resource.service.tagclassify.TagClassifyService;

@Tag(name = "管理后台 - 标签分类")
@RestController
@RequestMapping("/lm/tag-classify")
@Validated
public class TagClassifyController {

    @Resource
    private TagClassifyService tagClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建标签分类")
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:create')")
    public CommonResult<Integer> createTagClassify(@Valid @RequestBody TagClassifySaveReqVO createReqVO) {
        return success(tagClassifyService.createTagClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签分类")
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:update')")
    public CommonResult<Boolean> updateTagClassify(@Valid @RequestBody TagClassifySaveReqVO updateReqVO) {
        tagClassifyService.updateTagClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:delete')")
    public CommonResult<Boolean> deleteTagClassify(@RequestParam("id") Integer id) {
        tagClassifyService.deleteTagClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:query')")
    public CommonResult<TagClassifyRespVO> getTagClassify(@RequestParam("id") Integer id) {
        TagClassifyDO tagClassify = tagClassifyService.getTagClassify(id);
        return success(BeanUtils.toBean(tagClassify, TagClassifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签分类分页")
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:query')")
    public CommonResult<PageResult<TagClassifyRespVO>> getTagClassifyPage(@Valid TagClassifyPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<TagClassifyDO> pageResult = tagClassifyService.getTagClassifyPage(pageReqVO);
        PageResult<TagClassifyRespVO> responseVo = BeanUtils.toBean(pageResult, TagClassifyRespVO.class);
        tagClassifyService.setParentClassify(responseVo.getList());
        return success(responseVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签分类 Excel")
    @PreAuthorize("@ss.hasPermission('lm:tag-classify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagClassifyExcel(@Valid TagClassifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TagClassifyDO> list = tagClassifyService.getTagClassifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签分类.xls", "数据", TagClassifyRespVO.class,
                        BeanUtils.toBean(list, TagClassifyRespVO.class));
    }

}